package br.com.ac.fatureweb.utilitary.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import br.com.ac.fatureweb.utilitary.SpecialCharFilter;

@FacesConverter(forClass=String.class)
public class StringConverter implements Converter {

	private static final String LOWER_CASE = "lower";
	
	private static final String NONE_CASE = "none";

	/*
	 * (non-Javadoc)
	 * @see javax.faces.convert.Converter#getAsObject(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.String)
	 */
	@Override
	public Object getAsObject(FacesContext context, UIComponent component,  String string) {
		if(string == null || string.isEmpty()){ 
			return null;
		}
		
		if ( component.getId().contains(NONE_CASE) || component.getRendererType().contains("Secret")){
			return SpecialCharFilter.replaceSpecialChar(string);
		}
		else if ( component.getId().contains(LOWER_CASE) )
		{
			return SpecialCharFilter.replaceSpecialChar(string.toLowerCase());
		}
		else{
			return SpecialCharFilter.replaceSpecialChar(string.toUpperCase());
		}
	}

	/*
	 * (non-Javadoc)
	 * @see javax.faces.convert.Converter#getAsString(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.Object)
	 */
	@Override
	public String getAsString(FacesContext context, UIComponent component, Object object) {        
		if(object == null){
			return null;
		}
		
		if ( component.getId().contains(NONE_CASE) || component.getRendererType().contains("Secret")){
			return (String) object;
		}
		else if ( component.getId().contains(LOWER_CASE) ){
			return ((String) object).toLowerCase();
		}
		else{
			return ((String) object).toUpperCase();
		}
	}
}
